package com.tjetc.view.teacherview;

import cn.hutool.json.JSONUtil;
import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.TestPaper;
import com.zhiyun.domain.TestQuestion;
import com.zhiyun.service.TestPaperService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Exams_In {
    TestPaperService testPaperService= CilentProxy.getProxyInstance(TestPaperService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("~~~~~~~~~~~我的题库~~~~~~~~~~");
            System.out.println("1. 创建题库");
            System.out.println("2. 删除题库");
            System.out.println("3. 修改题库");
            System.out.println("4. 查看题库");
            System.out.println("0. 返回上一级");

            int i = scanner.nextInt();
            switch (i){
                case 1:
                    createTestPaper(course);
                    break;
                case 2:
                    deleteTestPaper(course);
                    break;
                case 3:
                    updateTestPaper(course);
                    break;
                case 4:
                    enterTestPaper(course);

                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void enterTestPaper(Course course) {
        while (true){
            Result result = testPaperService.selectAllTestPaper(AppData.teacher.getId());
            if (result.getCode()==200){
                List<TestPaper> obj = (List<TestPaper>) result.getObj();
                for (TestPaper testPaper : obj) {
                    System.out.println("题库id"+testPaper.getId()+"   题库名称"+testPaper.getName());
                }
                TestPaper testPaper = null;
                System.out.println("请输入想要进入的题库id");
                int i = scanner.nextInt();
                for (TestPaper testPapers : obj) {
                    if (i==testPapers.getId()){
                        testPaper=testPapers;
                    }
                }
                if (testPaper!=null){
                    new MyCourseView_In_Exams_In_TestPaper().start(course,testPaper);
                }

            }
            break;
        }
    }

    private void createTestPaper(Course course) {
        while (true){
            System.out.println("请输入题库名称");
            String name = scanner.next();
            TestPaper testPaper = new TestPaper();
            testPaper.setName(name);
            testPaper.setCreateTime(new Date());
            testPaper.setTeacherId(AppData.teacher.getId());
            testPaper.setMaxScore(0);
            testPaper.setTestQuestions(new ArrayList<TestQuestion>());
            Result result = testPaperService.insertTestPaper(testPaper);
            System.out.println(result.getMsg());
            break;
        }
    }

    private void deleteTestPaper(Course course) {
        while (true){
            Result result = testPaperService.selectAllTestPaper(AppData.teacher.getId());
            if (result.getCode()==200){
                List<TestPaper> obj = (List<TestPaper>) result.getObj();
                for (TestPaper testPaper : obj) {
                    System.out.println("题库id"+testPaper.getId()+"   题库名称"+testPaper.getName());
                }
                System.out.println("请输入想要删除的题库id");
                int i = scanner.nextInt();
                Result result1 = testPaperService.deleteTestPaper(i);
                System.out.println(result1.getMsg());
            }
            break;

        }
    }

    private void updateTestPaper(Course course) {
        while (true){
            Result result = testPaperService.selectAllTestPaper(AppData.teacher.getId());
            if (result.getCode()==200){
                List<TestPaper> obj = (List<TestPaper>) result.getObj();
                for (TestPaper testPaper : obj) {
                    System.out.println("题库id"+testPaper.getId()+"   题库名称"+testPaper.getName());
                }
                TestPaper testPaper = null;
                System.out.println("请输入想要修改的题库id");
                int i = scanner.nextInt();
                for (TestPaper testPapers : obj) {
                    if (i==testPapers.getId()){
                        testPaper=testPapers;
                    }
                }
                if (testPaper!=null){
                    System.out.println("请输入修改后的题库名称");
                    String name = scanner.next();
                    testPaper.setName(name);
                    Result result1 = testPaperService.updateTestPaper(testPaper);
                    System.out.println(result1.getMsg());
                }

            }
            break;

        }
    }
}
